Micron Document
`:top
In `F33f`_`[computer programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_programming]`_`f, a `!callback`! is a `F33f`_`[function`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_(computer_programming)]`_`f that is stored as data (a `F33f`_`[reference`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Reference_(computer_science)]`_`f) and designed to be called by another function – often `*back`* to the original `F33f`_`[abstraction layer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Abstraction_(computer_science)]`_`f.A function that accepts a callback `F33f`_`[parameter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Parameter_(computer_programming)]`_`f may be designed to call back before `F33f`_`[returning`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Return_statement]`_`f to its caller which is known as `*`F33f`_`[synchronous`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Synchronization_(computer_science)]`_`f`* or `*blocking`*.The function that accepts a callback may be designed to store the callback so that it can be called back after returning which is known as `*asynchronous`*, `*`F33f`_`[non-blocking`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Non-blocking_algorithm]`_`f`* or `*deferred`*.

`F33f`_`[Programming languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_languages]`_`f support callbacks in different ways such as `F33f`_`[function pointers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_pointers]`_`f, `F33f`_`[lambda expressions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lambda_(programming)]`_`f and `F33f`_`[blocks`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Block_(programming)]`_`f.A callback can be likened to leaving instructions with a tailor for what to do when a suit is ready, such as calling a specific phone number or delivering it to a given address.These instructions represent a callback: a function provided in advance to be executed later, often by a different part of the system and not necessarily by the one that received it.The term `*callback`* can be misleading, as it does not necessarily imply a return to the original caller, unlike a `F33f`_`[telephone callback`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Callback_(telecommunications)]`_`f.Mesa programming language formalised the callback mechanism used in Programming Languages. By passing a procedure as a parameter, Mesa essentially delegated the execution of that procedure to a later point in time when a specific event occurred, similar to how callbacks are implemented in modern programming languages.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]

>>Contents

• `F0af`_`[Use`#use]`_`f
• `F0af`_`[Event handling`#event-handling]`_`f
• `F0af`_`[Asynchronous action`#asynchronous-action]`_`f
• `F0af`_`[Polymorphism`#polymorphism]`_`f
• `F0af`_`[Implementation`#implementation]`_`f
• `F0af`_`[Example code`#example-code]`_`f
• `F0af`_`[C`#c]`_`f
• `F0af`_`[C++`#c]`_`f
• `F0af`_`[C#`#c]`_`f
• `F0af`_`[Kotlin`#kotlin]`_`f
• `F0af`_`[JavaScript`#javascript]`_`f
• `F0af`_`[Red and REBOL`#red-and-rebol]`_`f
• `F0af`_`[Rust`#rust]`_`f
• `F0af`_`[Lua`#lua]`_`f
• `F0af`_`[Python`#python]`_`f
• `F0af`_`[Julia`#julia]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>Use

A blocking callback runs in the `F33f`_`[execution`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Execution_(computing)]`_`f context of the function that passes the callback. A deferred callback can run in a different context such as during `F33f`_`[interrupt`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Interrupt]`_`f or from a `F33f`_`[thread`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread_(computing)]`_`f. As such, a deferred callback can be used for synchronization and delegating work to another thread.

>>>Event handling

A callback can be used for event handling. Often, consuming code registers a callback for a particular type of event. When that event occurs, the callback is called.Callbacks are often used to program the `F33f`_`[graphical user interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Graphical_user_interface]`_`f (GUI) of a program that runs in a `F33f`_`[windowing system`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Windowing_system]`_`f. The application supplies a reference to a custom callback function for the windowing system to call. The windowing system calls this function to notify the application of events like `F33f`_`[mouse`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_mouse]`_`f clicks and `F33f`_`[key`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_keyboard]`_`f presses.

>>>Asynchronous action

A callback can be used to implement asynchronous processing.

A caller requests an action and provides a callback to be called when the action completes which might be long after the request is made.

>>>Polymorphism

A callback can be used to implement `F33f`_`[polymorphism`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Polymorphism_(computer_science)]`_`f. In the following pseudocode, `B100`F9d9say_hi`f`b can take either `B100`F9d9write_status`f`b or `B100`F9d9write_error`f`b.

`B100`F9d9def write_status(message: str):`f`b
`B100`F9d9 write(stdout, message)`f`b
`B100`F9d9`f`b
`B100`F9d9def write_error(message: str):`f`b
`B100`F9d9 write(stderr, message)`f`b
`B100`F9d9`f`b
`B100`F9d9def say_hi(write):`f`b
`B100`F9d9 write("Hello world")`f`b

>>Implementation

The callback technology is implemented differently by `F33f`_`[programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f.

In `F33f`_`[assembly`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Assembly_language]`_`f, `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f, `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f, `F33f`_`[Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pascal_(programming_language)]`_`f, `F33f`_`[Modula2`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Modula2]`_`f and other languages, a callback function is stored internally as a `F33f`_`[function pointer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_pointer]`_`f. Using the same storage allows different languages to directly share callbacks without a `F33f`_`[design-time or runtime`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Program_lifecycle_phase]`_`f `F33f`_`[interoperability`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Interoperability]`_`f `F33f`_`[layer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Abstraction_layer]`_`f. For example, the `F33f`_`[Windows API`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Windows_API]`_`f is accessible via multiple languages, compilers and assemblers.C++ also allows objects to provide an implementation of the function call operation. The `F33f`_`[Standard Template Library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Standard_Template_Library]`_`f accepts these objects (called `*`F33f`_`[functors`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_object]`_`f`*) as parameters.Many `F33f`_`[dynamic languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dynamic_programming_language]`_`f, such as `F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f, `F33f`_`[Lua`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lua_(programming_language)]`_`f, `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f, `F33f`_`[Perl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Perl]`_`f`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f] and `F33f`_`[PHP`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=PHP]`_`f, allow a function object to be passed.`F33f`_`[CLI languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=List_of_CLI_languages]`_`f such as `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f and `F33f`_`[VB.NET`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=VB.NET]`_`f provide a `F33f`_`[type-safe`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_safety]`_`f encapsulating function reference known as `F33f`_`[delegate`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Delegate_(CLI)]`_`f.Events and `F33f`_`[event handlers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Event_handlers]`_`f, as used in .NET languages, provide for callbacks.Functional languages generally support `F33f`_`[first-class functions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=First-class_functions]`_`f, which can be passed as callbacks to other functions, stored as data or returned from functions.

Many languages, including Perl, Python, `F33f`_`[Ruby`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ruby_(programming_language)]`_`f, `F33f`_`[Smalltalk`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Smalltalk]`_`f, `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f (11+), C# and VB.NET (new versions) and most functional languages, support `F33f`_`[lambda expressions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lambda_(programming)]`_`f, unnamed functions with inline syntax, that generally acts as callbacks..In some languages, including `F33f`_`[Scheme`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scheme_(programming_language)]`_`f, `F33f`_`[ML`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ML_(programming_language)]`_`f, JavaScript, Perl, Python, Smalltalk, PHP (since 5.3.0),`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f] C++ (11+), Java (since 8),`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f] and many others, a lambda can be a `F33f`_`[closure`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Closure_(computer_science)]`_`f, i.e. can access variables locally defined in the context in which the lambda is defined.In an `F33f`_`[object-oriented programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f language such as `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f versions before function-valued arguments, the behavior of a callback can be achieved by passing an object that implements an interface. The methods of this object are callbacks.In `F33f`_`[PL/I`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=PL/I]`_`f and `F33f`_`[ALGOL 60`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ALGOL_60]`_`f a callback procedure may need to be able to access local variables in containing blocks, so it is called through an `*entry variable`* containing both the entry point and context information. `:cite-ref-6[`F5bf`_`[6`#cite-note-6]`_`f]

>>Example code

>>>C

Callbacks have a wide variety of uses, for example in error signaling: a `F33f`_`[Unix`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Unix]`_`f program might not want to terminate immediately when it receives `F33f`_`[SIGTERM`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SIGTERM]`_`f, so to make sure that its termination is handled properly, it would register the cleanup function as a callback. Callbacks may also be used to control whether a function acts or not: `F33f`_`[Xlib`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Xlib]`_`f allows custom predicates to be specified to determine whether a program wishes to handle an event.In the following `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f code, function `B100`F9d9print_number`f`b uses parameter `B100`F9d9get_number`f`b as a blocking callback. `B100`F9d9print_number`f`b is called with `B100`F9d9get_answer_to_most_important_question`f`b which acts as a callback function. When run the output is: "Value: 42".

`B100`F9d9#include <stdio.h>`f`b
`B100`F9d9#include <stdlib.h>`f`b
`B100`F9d9`f`b
`B100`F9d9void print_number(int (*get_number)(void)) {`f`b
`B100`F9d9 int val = get_number();`f`b
`B100`F9d9 printf("Value: %d\\n", val);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int get_answer_to_most_important_question(void) {`f`b
`B100`F9d9 return 42;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int main(void) {`f`b
`B100`F9d9 print_number(get_answer_to_most_important_question);`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b

>>>C++

In C++, `F33f`_`[functor`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_object]`_`f can be used in addition to function pointer.

>>>C#

In the following `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f code, method `B100`F9d9Helper.Method`f`b uses parameter `B100`F9d9callback`f`b as a blocking callback. `B100`F9d9Helper.Method`f`b is called with `B100`F9d9Log`f`b which acts as a callback function. When run, the following is written to the console: "Callback was: Hello world".

`B100`F9d9public class MainClass`f`b
`B100`F9d9{`f`b
`B100`F9d9 static void Main(string[] args)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 Helper helper = new Helper();`f`b
`B100`F9d9 helper.Method(Log);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 static void Log(string str)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 Console.WriteLine($"Callback was: {str}");`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9public class Helper`f`b
`B100`F9d9{`f`b
`B100`F9d9 public void Method(Action<string> callback)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 callback("Hello world");`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

>>>Kotlin

In the following `F33f`_`[Kotlin`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Kotlin_(programming_language)]`_`f code, function `B100`F9d9askAndAnswer`f`b uses parameter `B100`F9d9getAnswer`f`b as a blocking callback. `B100`F9d9askAndAnswer`f`b is called with `B100`F9d9getAnswerToMostImportantQuestion`f`b which acts as a callback function. Running this will tell the user that the answer to their question is "42".

`B100`F9d9fun main() {`f`b
`B100`F9d9 print("Enter the most important question: ")`f`b
`B100`F9d9 val question = readLine()`f`b
`B100`F9d9 askAndAnswer(question, ::getAnswerToMostImportantQuestion)`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9fun getAnswerToMostImportantQuestion(): Int {`f`b
`B100`F9d9 return 42`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9fun askAndAnswer(question: String?, getAnswer: () -> Int) {`f`b
`B100`F9d9 println("Question: $question")`f`b
`B100`F9d9 println("Answer: ${getAnswer()}")`f`b
`B100`F9d9}`f`b

>>>JavaScript

In the following `F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f code, function `B100`F9d9calculate`f`b uses parameter `B100`F9d9operate`f`b as a blocking callback. `B100`F9d9calculate`f`b is called with `B100`F9d9multiply`f`b and then with `B100`F9d9sum`f`b which act as callback functions.

`B100`F9d9function calculate(a, b, operate) {`f`b
`B100`F9d9 return operate(a, b);`f`b
`B100`F9d9}`f`b
`B100`F9d9function multiply(a, b) {`f`b
`B100`F9d9 return a * b;`f`b
`B100`F9d9}`f`b
`B100`F9d9function sum(a, b) {`f`b
`B100`F9d9 return a + b;`f`b
`B100`F9d9}`f`b
`B100`F9d9// outputs 20`f`b
`B100`F9d9alert(calculate(10, 2, multiply));`f`b
`B100`F9d9// outputs 12`f`b
`B100`F9d9alert(calculate(10, 2, sum));`f`b

The collection method `B100`F9d9.each()`f`b of the `F33f`_`[jQuery`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JQuery]`_`f `F33f`_`[library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript_libraries]`_`f uses the function passed to it as a blocking callback. It calls the callback for each item of the collection. For example:

`B100`F9d9$("li").each(function(index) {`f`b
`B100`F9d9 console.log(index + ": " + $(this).text());`f`b
`B100`F9d9});`f`b

Deferred callbacks are commonly used for handling events from the user, the client and timers. Examples can be found in `B100`F9d9addEventListener`f`b, `F33f`_`[Ajax`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ajax_(programming)]`_`f and `B100`F9d9`F33f`_`[XMLHttpRequest`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=XMLHttpRequest]`_`f`f`b. `:cite-ref-7[`F5bf`_`[7`#cite-note-7]`_`f]

In addition to using callbacks in JavaScript source code, C functions that take a function are supported via js-ctypes.`:cite-ref-8[`F5bf`_`[8`#cite-note-8]`_`f]

>>>Red and REBOL

The following `F33f`_`[REBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=REBOL]`_`f/`F33f`_`[Red`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Red_(programming_language)]`_`f code demonstrates callback use.

• As alert requires a string, form produces a string from the result of calculate
• The get-word! values (i.e., :calc-product and :calc-sum) trigger the interpreter to return the code of the function rather than evaluate with the function.
• The datatype! references in a block! [float! integer!] restrict the type of values passed as arguments.

`B100`F9d9Red [Title: "Callback example"]`f`b
`B100`F9d9`f`b
`B100`F9d9calculate: func [`f`b
`B100`F9d9 num1 [number!]`f`b
`B100`F9d9 num2 [number!]`f`b
`B100`F9d9 callback-function [function!]`f`b
`B100`F9d9][`f`b
`B100`F9d9 callback-function num1 num2`f`b
`B100`F9d9]`f`b
`B100`F9d9`f`b
`B100`F9d9calc-product: func [`f`b
`B100`F9d9 num1 [number!]`f`b
`B100`F9d9 num2 [number!]`f`b
`B100`F9d9][`f`b
`B100`F9d9 num1 * num2`f`b
`B100`F9d9]`f`b
`B100`F9d9`f`b
`B100`F9d9calc-sum: func [`f`b
`B100`F9d9 num1 [number!]`f`b
`B100`F9d9 num2 [number!]`f`b
`B100`F9d9][`f`b
`B100`F9d9 num1 + num2`f`b
`B100`F9d9]`f`b
`B100`F9d9`f`b
`B100`F9d9; alerts 75, the product of 5 and 15`f`b
`B100`F9d9alert form calculate 5 15 :calc-product`f`b
`B100`F9d9`f`b
`B100`F9d9; alerts 20, the sum of 5 and 15`f`b
`B100`F9d9alert form calculate 5 15 :calc-sum`f`b

>>>Rust

`F33f`_`[Rust`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rust_(programming_language)]`_`f have the `B100`F9d9Fn`f`b, `B100`F9d9FnMut`f`b and `B100`F9d9FnOnce`f`b traits.`:cite-ref-9[`F5bf`_`[9`#cite-note-9]`_`f]

`B100`F9d9fn call_with_one<F>(func: F) -> usize`f`b
`B100`F9d9 where F: Fn(usize) -> usize {`f`b
`B100`F9d9 func(1)`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9let double = |x| x * 2;`f`b
`B100`F9d9assert_eq!(call_with_one(double), 2);`f`b

>>>Lua

In this `F33f`_`[Lua`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lua]`_`f code, function `B100`F9d9calculate`f`b accepts the `B100`F9d9operation`f`b parameter which is used as a blocking callback. `B100`F9d9calculate`f`b is called with both `B100`F9d9add`f`b and `B100`F9d9multiply`f`b, and then uses an `F33f`_`[anonymous function`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Anonymous_function]`_`f to divide.

`B100`F9d9function calculate(a, b, operation)`f`b
`B100`F9d9 return operation(a, b)`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9function multiply(a, b)`f`b
`B100`F9d9 return a * b`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9function add(a, b)`f`b
`B100`F9d9 return a + b`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9print(calculate(10, 20, multiply)) -- outputs 200`f`b
`B100`F9d9print(calculate(10, 20, add)) -- outputs 30`f`b
`B100`F9d9-- an example of a callback using an anonymous function`f`b
`B100`F9d9print(calculate(10, 20, function(a, b)`f`b
`B100`F9d9 return a / b -- outputs 0.5`f`b
`B100`F9d9end))`f`b

>>>Python

In the following `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f code, function `B100`F9d9calculate`f`b accepts a parameter `B100`F9d9operate`f`b that is used as a blocking callback. `B100`F9d9calculate`f`b is called with `B100`F9d9square`f`b which acts as a callback function.

`B100`F9d9def square(val):`f`b
`B100`F9d9 return val ** 2`f`b
`B100`F9d9`f`b
`B100`F9d9def calculate(operate, val):`f`b
`B100`F9d9 return operate(val)`f`b
`B100`F9d9`f`b
`B100`F9d9calculate(square, 5) # outputs: 25`f`b

>>>Julia

In the following `F33f`_`[Julia`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Julia_(programming_language)]`_`f code, function `B100`F9d9calculate`f`b accepts a parameter `B100`F9d9operate`f`b that is used as a blocking callback. `B100`F9d9calculate`f`b is called with `B100`F9d9square`f`b which acts as a callback function.

`B100`F9d9julia> square(val) = val^2`f`b
`B100`F9d9square (generic function with 1 method)`f`b
`B100`F9d9julia> calculate(operate, val) = operate(val)`f`b
`B100`F9d9calculate (generic function with 1 method)`f`b
`B100`F9d9julia> calculate(square, 5)`f`b
`B100`F9d925`f`b

>>See also

• `F33f`_`[Command pattern`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Command_pattern]`_`f
• `F33f`_`[Continuation-passing style`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Continuation-passing_style]`_`f
• `F33f`_`[Event loop`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Event_loop]`_`f
• `F33f`_`[Event-driven programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Event-driven_programming]`_`f
• `F33f`_`[Implicit invocation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Implicit_invocation]`_`f
• `F33f`_`[Inversion of control`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Inversion_of_control]`_`f
• `F33f`_`[libsigc++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Libsigc++]`_`f, a callback library for C++
• `F33f`_`[Signals and slots`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Signals_and_slots]`_`f
• `F33f`_`[User exit`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=User_exit]`_`f

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f `:citerefe-sweet1985`aE. Sweet, Richard (1985). "The Mesa Programming Environment". `*ACM SIGPLAN Notices`*. `!20`! (7): 216–229. `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1145/17919.806843.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Perl Cookbook - 11.4. Taking References to Functions". 2 July 1999. Retrieved 2008-03-03.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "Advanced Perl Programming - 4.2 Using Subroutine References". 2 July 1999. Retrieved 2008-03-03.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f "PHP Language Reference - Anonymous functions". Retrieved 2011-06-08.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "What's New in JDK 8". `*oracle.com`*.
`:cite-note-6`!6.`! `F0af`_`[↑`#cite-ref-6]`_`f `:citerefbelzerholzmankent1979`aBelzer, Jack; Holzman, Albert G; Kent, Allen, eds. (1979). `*Encyclopedia of Computer Science and Technology: Volume 12`*. Marcel Dekker, inc. p. 164. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-8247-2262-0. Retrieved January 28, 2024.
`:cite-note-7`!7.`! `F0af`_`[↑`#cite-ref-7]`_`f "Creating JavaScript callbacks in components". Archive. `*UDN Web Docs`* (Documentation page). sec. JavaScript functions as callbacks. Archived from the original on 2021-12-16. Retrieved 2021-12-16.
`:cite-note-8`!8.`! `F0af`_`[↑`#cite-ref-8]`_`f `:citerefholleyshepherd`aHolley, Bobby; Shepherd, Eric (eds.). "Declaring and Using Callbacks". Docs. `*`F33f`_`[Mozilla Developer Network`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mozilla_Developer_Network]`_`f`* (Documentation page). Archived from the original on 2019-01-17. Retrieved 2021-12-16.
`:cite-note-9`!9.`! `F0af`_`[↑`#cite-ref-9]`_`f "Fn in std::ops - Rust". `*doc.rust-lang.org`*. Retrieved 18 January 2025.

>>External links

• Basic Instincts: Implementing Callback Notifications Using Delegates - `F33f`_`[MSDN Magazine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=MSDN_Magazine]`_`f, December 2002
• Implement callback routines in Java
• Implement Script Callback Framework in ASP.NET 1.x - Code Project, 2 August 2004
• Interfacing C++ member functions with C libraries (archived from the original on July 6, 2011)
• Style Case Study #2: Generic Callbacks

`c`F0af`_`[↑ Back to top`#top]`_`f`a